package com.rbs.controller;

import com.rbs.entity.Book;
import com.rbs.entity.Category;
import com.rbs.service.BookService;
import com.rbs.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/selectByRecommend")
    public String selectByRecommend(Model model){
        List<Book> bookList = bookService.selectByRecommend();
        List<Book> likeList = bookService.selectByLike();
        List<Book> newList = bookService.selectByNew();
        List<Book> newLikeList = bookService.selectByNewAndLike();
        List<Category> categories = categoryService.selectAll();
        model.addAttribute("categories",categories);
        model.addAttribute("bookList",bookList);
        model.addAttribute("likeList",likeList);
        model.addAttribute("newList",newList);
        model.addAttribute("newLikeList",newLikeList);
        return "forward:/main/main.jsp";
    }
    @RequestMapping("/selectByLevelTow")
    public String selectByLevelTow(int cid,Model model){
        List<Book> bookList = bookService.selectByLevelTow(cid);
        model.addAttribute("bookList",bookList);
        List<Category> categories = categoryService.selectByLevelTwo(cid);
        model.addAttribute("categories",categories);
        return "forward:/main/book_list.jsp";
    }
    @RequestMapping("/selectByLevelOne")
    public String selectByLevelOne(int cid,Model model){
        List<Book> bookList = bookService.selectByLevelOne(cid);
        List<Category> categories = categoryService.selectByLevelOne(cid);
        model.addAttribute("categories",categories);
        model.addAttribute("bookList",bookList);
        return "forward:/main/book_list.jsp";
    }
    @RequestMapping("/selectBookById")
    public String selectBookById(Integer bookId,Model model){
        Book book = bookService.selectBookById(bookId);
        model.addAttribute("book",book);
        return "forward:/main/product.jsp";
    }
}
